<template>
  <div class="createPost-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="rules"
      class="form-container"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button
          v-loading="loading"
          style="margin-left: 10px"
          type="success"
          @click="submitForm"
        >
          提交
        </el-button>
        <router-link :to="'/category/categorylist'">
          <el-button
            class="filter-item"
            style="margin-left: 10px"
            v-loading="loading"
            type="warning"
          >
            返回
          </el-button>
        </router-link>
      </sticky>

      <div class="createPost-main-container">
        <Warning />
        <el-row>
          <el-col :span="24">
            <div class="postInfo-container">
              <el-row>
                <el-col :span="8">
                  <el-form-item
                    label-width="90px"
                    label="分类名称:"
                    class="postInfo-container-item"
                    prop="category_name"
                  >
                    <el-input v-model="postForm.category_name"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </div>
</template>

<script>
import Warning from "./Warning";
import Sticky from "@/components/Sticky"; // 粘性header组件
// import { fetchRoad, createRoad, updateRoad, personnelList } from '@/api/street/road'
import {
  addCategory,
  getInfo,
  updateCategory,
} from "@/api/category";

const defaultForm = {
  category_name: "",
  id: undefined,
};

export default {
  name: "Detail",
  components: { Sticky, Warning },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        // 规则  required 是否必填  message 规则不通过显示的内容  trigger 触发规则方案 blur 失去焦点时触发
        category_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      tempRoute: {},
      list: [],
      personnelid: [],
    };
  },
  computed: {},
  created() {
    // 如果是修改，则获取api的内容
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id;
      this.fetchData(id);
    }

    this.tempRoute = Object.assign({}, this.$route);
  },
  methods: {
    fetchData(id) {
      getInfo({ id: id })
        .then((response) => {
          this.postForm.category_name = response.data.title;
          this.postForm.id = response.data.id;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    submitForm() {
      this.$refs.postForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.isEdit) {
            updateCategory({
              id: this.postForm.id,
              title: this.postForm.category_name,
            }).then((response) => {
              this.$notify({
                title: "成功",
                message: "修改分类成功",
                type: "success",
                duration: 2000,
              });
              this.$router.push("/category/categorylist");
            });
          } else {
            addCategory({ title: this.postForm.category_name }).then(
              (response) => {
                this.$notify({
                  title: "成功",
                  message: "新建分类成功",
                  type: "success",
                  duration: 2000,
                });
                this.$router.push("/category/categorylist");
              }
            );
          }
          this.postForm.status = "published";
          this.loading = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
